package components.contorls.superClass
{
    import components.core.UIComponent;
    import components.utils.EventListenerUtil;
    
    import flash.events.Event;
    import flash.events.MouseEvent;

    /**
     *翻页
     * @author zn
     *
     */
    [Event(name="change", type="flash.events.Event")]
    public class BaseTurnPage extends UIComponent
    {
        /**
         *每页数量
         */
        private var _pageCount:uint = 3;

        /**
         *当前页数
         */
        private var _currentPageIndex:uint = 1;

        /**
         *最大页数
         */
        private var _maxPage:uint = 1;

        /**
         *数据长度
         */
        private var _dataLength:uint = 1;

        public function BaseTurnPage()
        {
            super();

			EventListenerUtil.addEventListener(prevButton,MouseEvent.CLICK, turnPageHandler);
			EventListenerUtil.addEventListener(nextButton,MouseEvent.CLICK, turnPageHandler);
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get pageLabel():BaseLabel
        {
            return getChildByName("pageLabelSkin") as BaseLabel;
        }

        public function get prevButton():BaseButton
        {
            return getChildByName("prevBtn") as BaseButton;
        }

        public function get nextButton():BaseButton
        {
            return getChildByName("nextBtn") as BaseButton;
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();

            if (!_initialized)
            {
                pageLabel.verticalCenter = 0;
                pageLabel.left = prevButton.width;
                pageLabel.right = nextButton.width;

                prevButton.left = 0;
                nextButton.right = 0;
            }

            if (pageLabel)
                pageLabel.text = _currentPageIndex + "/" + _maxPage;
        }

        protected override function measureSizes():Boolean
        {
            return super.measureSizes();
        }

        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
			
			if(dataLength==0 || maxPage<2)
				visible=false;
			else
				visible=true;
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/

        /**
         * 每页数量
         */
        [Inspectable(defaultValue="3")]
        public function get pageCount():uint
        {
            return _pageCount;
        }

        public function set pageCount(value:uint):void
        {
            _pageCount = value;
            calculatePage();
            invalidate();
        }

        /**
         * 数据长度
         */
        [Inspectable(defaultValue="1")]
        public function get dataLength():uint
        {
            return _dataLength;
        }

        public function set dataLength(value:uint):void
        {
            _dataLength = value;
            calculatePage();
            updateBtnState();
            invalidate();
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/


        /**
         *计算页面
         *
         */
        protected function calculatePage():void
        {
            _maxPage = Math.ceil(dataLength / _pageCount);
            _maxPage = Math.max(1, _maxPage);

            _currentPageIndex = Math.min(1, _maxPage);
        }

        /**
         *翻页事件
         * @param event
         *
         */
        protected function turnPageHandler(event:MouseEvent):void
        {
            var isUp:Boolean = event.currentTarget == prevButton ? true : false;

            var oldPage:int = _currentPageIndex;

            if (isUp)
                _currentPageIndex -= 1;
            else
                _currentPageIndex += 1;

            checkCurrentPage();

            updateBtnState();

            if (oldPage != _currentPageIndex)
                dispatchEvent(new Event(Event.CHANGE));

            invalidate();
        }

        private function updateBtnState():void
        {
            if (currentPageIndex <= 1)
                prevButton.enabled = false;
            else
                prevButton.enabled = true;

            if (currentPageIndex == _maxPage)
                nextButton.enabled = false;
            else
                nextButton.enabled = true;
        }


        /**
         *当前页数
         */
        public function get currentPageIndex():uint
        {
            return _currentPageIndex;
        }

		public function set currentPageIndex(value:uint):void
		{
			_currentPageIndex=value;
			checkCurrentPage();
			
			updateBtnState();
			invalidate();
		}
		
        /**
         *验证当前页面
         *
         */
        protected function checkCurrentPage():void
        {
            _currentPageIndex = Math.max(_currentPageIndex, 1);
            _currentPageIndex = Math.min(_currentPageIndex, _maxPage);
        }

        public override function destroy():void
        {
            super.destroy();

        }

        public override function validateNow():void
        {
            super.validateNow();
            pageLabel.validateNow();
        }

        public function get startIndex():uint
        {
            return Math.max(0, (currentPageIndex - 1)) * pageCount;
        }

        public function get endIndex():uint
        {
            return Math.min(currentPageIndex * pageCount, dataLength);
        }

        /**
         *最大页数
         */
        public function get maxPage():uint
        {
            return _maxPage;
        }

        protected override function frameConstructedHandler(event:Event):void
        {
            super.frameConstructedHandler(event);

            updateBtnState();
        }
    }
}